package lib.wzh.com.libcore.framework.response;

import com.fasterxml.jackson.databind.ser.Serializers;
import lib.wzh.com.libcore.framework.chain.ReturnBuilder;
import lib.wzh.com.libcore.framework.chain.ReturnChain;
import lib.wzh.com.libcore.framework.enums.ResponseEnum;
import lib.wzh.com.libcore.framework.interfaces.BaseEnum;

import java.io.Serializable;

/**
 * @Author lib
 * @Date 2021/11/19 10:05
 * @ClassName BaseResponse
 * @Version v1.0
 */

public class BaseResponse<T> implements Serializable {

    private String code;

    private String msg;

    private boolean success;

    private T data;

    public BaseResponse() {
        this(ResponseEnum.SUCCESS);
    }

    public BaseResponse(T data) {
        BaseResponse<T> br = ReturnBuilder.execute(data);
        this.code = br.getCode();
        this.msg = br.getMsg();
        this.success = br.isSuccess();
        this.data = br.getData();
    }

    public BaseResponse(String code, String msg, boolean success) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = null;
    }

    public BaseResponse(String code, String msg, boolean success, T data) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = data;
    }

    public BaseResponse(BaseEnum baseEnum) {
        this.code = baseEnum.code();
        this.msg = baseEnum.msg();
        this.success = baseEnum.success();
        this.data = null;
    }

    public String getCode() {
        return code;
    }

    public BaseResponse<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public BaseResponse<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public boolean isSuccess() {
        return success;
    }

    public BaseResponse<T> setSuccess(boolean success) {
        this.success = success;
        return this;
    }

    public T getData() {
        return data;
    }

    public BaseResponse<T> setData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public String toString() {
        return "{\"BaseResponse\":{"
                + "\"code\":\""
                + code + '\"'
                + ",\"msg\":\""
                + msg + '\"'
                + ",\"success\":"
                + success
                + ",\"data\":"
                + data
                + "}}";

    }
}
